<?php
/**
 * Created by PhpStorm.
 * User: Young<YoungShen@fih-foxconn.com.cn
 * Date: 2019/3/14
 * Time: 12:53
 */

namespace App\Http\Controllers\Mapi;


use FFMpeg\Media\AbstractMediaType;

class MergeAudio extends AbstractMediaType
{
    public function __construct(Audio $audio, FFMpegDriver $driver, FFProbe $ffprobe, $width, $height, $colors = array(self::DEFAULT_COLOR))
    {
        parent::__construct($audio->getPathfile(), $driver, $ffprobe);
        $this->audio = $audio;
        $this->width = $width;
        $this->height = $height;

        $this->setColors($colors);
    }

    public function filters()
    {
        // TODO: Implement filters() method.
    }

    public function save($pathfile)
    {
        /**
         * might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg
         * @see http://ffmpeg.org/ffmpeg.html#Main-options
         */
        $commands = array(
            '-y', '-i', $this->pathfile, '-filter_complex',
            'showwavespic=colors='.$this->compileColors().':s='.$this->width.'x'.$this->height,
            '-frames:v', '1'
        );

        foreach ($this->filters as $filter) {
            $commands = array_merge($commands, $filter->apply($this));
        }

        $commands = array_merge($commands, array($pathfile));

        try {
            $this->driver->command($commands);
        } catch (ExecutionFailureException $e) {
            $this->cleanupTemporaryFile($pathfile);
            throw new RuntimeException('Unable to save waveform', $e->getCode(), $e);
        }

        return $this;
    }
}
